package com.sixmac.controller;

import com.sixmac.annotation.AuthUser;
import com.sixmac.core.plugins.dao.Blade;
import com.sixmac.core.plugins.dao.Db;
import com.sixmac.core.toolbox.CMap;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.core.toolbox.grid.BladePage;
import com.sixmac.core.toolbox.kit.JsonKit;
import com.sixmac.core.toolbox.support.Convert;
import com.sixmac.model.ClienUser;
import com.sixmac.model.ExamineTopicError;
import com.sixmac.service.SimulateService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2018/7/12.
 */
@Controller
@RequestMapping("/api/examin")
public class ExaminController extends ApiController {
    @Autowired
    SimulateService simulateService;

    /**
     * @api {post} /api/examin/index    我的错题
     * @apiName examin.index
     * @apiGroup examin
     * @apiParam {String}  token  token
     * @apiSuccess {String} data.rows.total 错题总数
     * @apiSuccess {String} data.rows.ts 单项错题数量
     * @apiSuccess {String} data.rows.title 考试
     * @apiSuccess {String} data.rows.topicIds 考试ids
     * @apiError {Integer} 1
     */
    @AuthUser(isGuess = false)
    @RequestMapping("/index")
    public AjaxResult index(ClienUser user, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "6") Integer size) {
        Map map1 = Db.selectOne("select count(distinct  topicId) as 'c' from t_examin_topic_error where userId = #{userId} and deleteKey =2", getCMap().put("userId", user.getId()));
        map1.get("c");//我的错题数量
        List<Map> map2 = Db.selectList("select count(distinct topicId) as 'ts',group_concat(distinct topicId) as 'topicIds' ,e.title from t_examin e inner join t_examin_topic_error err on err.examinId = e.id  where err.userId = #{userId} and err.deleteKey =2 group by examinId", getCMap().put("userId", user.getId()));
        return json(getCMap().put("total", map1.get("c")).put("list", map2));
    }


    /**
     * @api {post} /api/examin/result   删除错误题目
     * @apiName examin.result
     * @apiGroup examin
     * @apiParam {String}  token  token
     * @apiParam {String}  id  考试题目id
     * @apiError {Integer} 1
     */
    @AuthUser(isGuess = false)
    @RequestMapping("/result")
    public AjaxResult result(ClienUser user, String id) {
        // Blade.create(ExamineTopicError.class).deleteBy("userId=#{userId} and topicId  =#{tid}", getCMap().put("userId", user.getId()).put("tid", s));
        List<ExamineTopicError> list = Blade.create(ExamineTopicError.class).findBy("userId = #{userId} and  topicId = #{topicId}", getCMap().put("userId", user.getId()).put("topicId", id));
        for (ExamineTopicError topicError : list) {
            topicError.setDeleteKey(1);
            Blade.create(ExamineTopicError.class).update(topicError);
        }
        return success("");
    }


    /**
     * @api {post} /api/examin/getExaminList   通过题目的ids返回所有题目(所有通过ids获取题目都用这个接口)
     * @apiName examin.getExaminList
     * @apiGroup examin
     * @apiDescription 批量返回所有题目lists集合
     * @apiParam {String}  token  token
     * @apiParam {String}  ids  考试题目的ids
     * @apiSuccess {String} data.rows.topic.choice 1单选 2多择题3判断题
     * @apiSuccess {String} data.rows.topic.reach 理由
     * @apiSuccess {String} data.rows.topic.image 题目图片为空则不显示，多个图片用,分隔
     * @apiSuccess {String} data.rows.topic.isTrue 正确答案，多选题目用,分隔
     * @apiSuccess {String} data.rows.topic.name 题目
     * @apiSuccess {String} data.rows.list.content 问题内容
     * @apiSuccess {String} data.rows.info.total 题目数量
     * @apiError {Integer} 1
     */
    @AuthUser(isGuess = false)
    @RequestMapping("/getExaminList")
    public AjaxResult getExaminList(ClienUser user, String ids) {
        AjaxResult res = new AjaxResult();
        // List<Map> list = Db.selectList(" select t.id,t.examineId,t.name,t.choice,t.image,t.reach,t.isTrue,a.content from t_examin_topic t inner join t_examin_topic_answer a on t.id=a.topicId  where 1=1 and t.id in (#{join(ids)})", CMap.init().put("ids", Convert.toStrArray(ids)));
        List<Map> list = Db.selectList("select * from t_examin_topic where id in  (#{join(ids)})", getCMap().put("ids", Convert.toIntArray(ids)));
        for (Map map : list) {
            List<Map> answers = Db.selectList("select content from t_examin_topic_answer where topicId =" + map.get("id"));
            map.put("answers", answers);
        }
        return res.setData(CMap.init().put("list", list).put("total", list.size()));
    }

    /**
     * @api {post} /api/examin/all   获取所有错题
     * @apiName examin.all
     * @apiGroup examin
     * @apiDescription 批量返回所有错题的ids
     * @apiParam {String}  token  token
     * @apiSuccess {String} ids 所有错题的ids
     * @apiError {Integer} 1
     */
    @AuthUser(isGuess = false)
    @RequestMapping("/all")
    public AjaxResult getAll(ClienUser user) {
        Map map = Db.selectOne("select group_concat(distinct topicId) as ids from t_examin_topic_error where userId =#{userId} and deleteKey =2 ", getCMap().put("userId", user.getId()));
        return json(map);
    }

    @Test
    public void te() {
        String s = "26";
        System.out.print(s.split(","));
    }


}
